package lumingweihua.future.cn.lumingweihua.network;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.network.OnHttpLoadingListener;


/**
 * Author        zhusx
 * Email         327270607@qq.com
 * Created       2016/10/13 9:20
 */
public abstract class LoadingBaseHelper<Id, Result, Parameter> implements OnHttpLoadingListener<Id, IHttpResult<Result>, Parameter> {
    private ViewGroup helperLayout;
    private View loadingView;
    private View errorView;
    private boolean isSuccess = false;
    protected View resLayout;
    private View emptyView;

    public LoadingBaseHelper(View resLayout) {
        this.resLayout = resLayout;
        ViewParent parent = resLayout.getParent();
        if (parent == null) {
            throw new IllegalStateException("resLayout 不能为null");
        }
        if (parent instanceof ViewGroup) {
            ViewGroup.LayoutParams lp = resLayout.getLayoutParams();
            LinearLayout parentLayout = new LinearLayout(resLayout.getContext());
            parentLayout.setOrientation(LinearLayout.VERTICAL);
            parentLayout.setLayoutParams(lp);
            ViewGroup group = (ViewGroup) parent;
            int index = group.indexOfChild(resLayout);
            group.removeView(resLayout);
            group.addView(parentLayout, index, lp);
            parentLayout.addView(resLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            parentLayout.addView(helperLayout = new FrameLayout(resLayout.getContext()), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            resLayout.setVisibility(View.GONE);
            parentLayout.invalidate();
        } else {
            throw new IllegalStateException("resLayout.getParent() 必须是ViewGroup!!");
        }
    }

    public final void _setEmptyView(View child) {
        if (emptyView != null) {
            helperLayout.removeView(emptyView);
        }
        if (child != null) {
            if (child.getParent() != null) {
                ((ViewGroup) child.getParent()).removeView(child);
            }
            helperLayout.addView(child, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            child.setVisibility(View.GONE);
        }
        emptyView = child;
    }

    public final void _setLoadingView(View child) {
        if (loadingView != null) {
            helperLayout.removeView(loadingView);
        }
        if (child != null) {
            if (child.getParent() != null) {
                ((ViewGroup) child.getParent()).removeView(child);
            }
            helperLayout.addView(child, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            child.setVisibility(View.GONE);
        }
        loadingView = child;
    }

    public final void _setErrorView(View child) {
        if (errorView != null) {
            helperLayout.removeView(errorView);
        }
        if (child != null) {
            if (child.getParent() != null) {
                ((ViewGroup) child.getParent()).removeView(child);
            }
            helperLayout.addView(child, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            child.setVisibility(View.GONE);
        }
        errorView = child;
    }

    public void __onError(View errorView, HttpRequest<Parameter> request, IHttpResult<Result> data, boolean isAPIError, String error_message) {
    }

    public abstract void __onComplete(HttpRequest<Parameter> request, IHttpResult<Result> data);

    @Override
    public void onLoadStart(Id id, HttpRequest<Parameter> request) {
        if (emptyView != null) {
            emptyView.setVisibility(View.GONE);
        }
        if (request.isRefresh || !isSuccess) {
            if (loadingView != null) {
                if (isShowLoading()) {
                    loadingView.setVisibility(View.VISIBLE);
                }
            }
            if (resLayout.getVisibility() == View.VISIBLE) {
                resLayout.setVisibility(View.GONE);
            }
            if (errorView != null && errorView.getVisibility() == View.VISIBLE) {
                errorView.setVisibility(View.GONE);
            }
        }
    }

    public boolean isShowLoading() {
        return true;
    }

    @Override
    public void onLoadError(Id id, HttpRequest<Parameter> request, IHttpResult<Result> data, boolean isAPIError, String error_message) {
        if (emptyView != null) {
            emptyView.setVisibility(View.GONE);
        }
        if (request.isRefresh || !isSuccess) {
            if (loadingView != null) {
                loadingView.setVisibility(View.GONE);
            }
            if (errorView != null) {
                errorView.setVisibility(View.VISIBLE);
            }
            if (resLayout.getVisibility() == View.VISIBLE) {
                resLayout.setVisibility(View.GONE);
            }
        }
        __onError(errorView, request, data, isAPIError, error_message);
    }

    @Override
    public void onLoadComplete(Id id, HttpRequest<Parameter> request, IHttpResult<Result> data) {
        if (request.isRefresh || !isSuccess) {
            if (loadingView != null) {
                loadingView.setVisibility(View.GONE);
            }
            if (resLayout.getVisibility() != View.VISIBLE) {
                resLayout.setVisibility(View.VISIBLE);
            }
        }
        isSuccess = true;
        __onComplete(request, data);
        if (emptyView != null) {
            if (resLayout instanceof RecyclerView) {
                RecyclerView recyclerView = (RecyclerView) resLayout;
                RecyclerView.Adapter adapter = recyclerView.getAdapter();
                if (adapter != null) {
                    int count = adapter.getItemCount();
                    if (count == 0) {
                        resLayout.setVisibility(View.GONE);
                        emptyView.setVisibility(View.VISIBLE);
                    } else {
                        emptyView.setVisibility(View.GONE);
                    }
                }
            } else {
                emptyView.setVisibility(View.GONE);
            }
        }
    }
}
